/**
 * Copyright 2015-2016 eyaomall.cn
 * All rights reserved.
 *
 * @project
 * @author jackey
 * @version 1.0
 * @date 2016-01-06
 */
package com.eyaomall.wxmanage.common.helper;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.eyaomall.wxmanage.common.enums.MemberCookieEnums;
import com.lycheeframework.core.common.cookie.CookieManager;
import com.lycheeframework.core.common.util.StringUtil;

public class MemberHelper {
	
	/**
	 * 获取会员ID
	 * @return
	 */
	public static Integer getId(){
		String memberId = getManager().getValue(MemberCookieEnums._MALL_FORM.getName(), MemberCookieEnums._MALL_FORM_MEMBER_ID.getName());
		if(StringUtil.isEmpty(memberId) || "null".equalsIgnoreCase(memberId)){
			return 0;
		}
		
		return StringUtil.isEmpty(memberId) ? 0 : Integer.parseInt(memberId);
	}
	
	
	/**
	 * 获取登陆的账号
	 * @return
	 */
	public static String getAccount(){
		
		return  getManager().getValue(MemberCookieEnums._MALL_FORM.getName(),MemberCookieEnums._MALL_FORM_ACCOUNT.getName());
	}
	
	
	/**
	 * 获取会员手机号码
	 * @return
	 */
	public static String getPhone(){
		
		return getManager().getValue(MemberCookieEnums._MALL_FORM.getName(), MemberCookieEnums._MALL_FORM_PHONE.getName());
	}
	
	
	

	/**
	 * 获取会员手机号码
	 * @return
	 */
	public static String getNickName(){
		
		return getManager().getValue(MemberCookieEnums._MALL_FORM.getName(), MemberCookieEnums._MALL_FORM_NICKNAME.getName());
	}
	
	
	
	

	
	

	public static HttpServletRequest getRequest() {
		
		return ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
	}
	
	public static HttpServletResponse getResponse() {
		
		return ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getResponse();
	}
	
	private static CookieManager getManager(){
		
		return new CookieManager(getRequest());
	}
	
	


	
	
	/**
	 * 获取最后操作日期
	 * @return
	 */
	public static String getLastTime(){
		String time = getManager().getValue(MemberCookieEnums._MALL_FORM_LAST_TIME.getName(), MemberCookieEnums._MALL_FORM_LAST_TIME.getName());
		
		return time;
	}
	
	/**
	 * 写入更新日期
	 */
	public static void writeLastTime(){
		CookieManager manager = new CookieManager(getRequest());
		manager.setValue(MemberCookieEnums._MALL_FORM_LAST_TIME.getName(), MemberCookieEnums._MALL_FORM_LAST_TIME.getName(), String.valueOf(new Date().getTime()),true);
		manager.save(getResponse(), MemberCookieEnums._MALL_FORM_LAST_TIME.getName(),true);
	}
	
	public static void cleanAll(){
		CookieManager manager = new CookieManager(getRequest());
		manager.cleanAll(getResponse(), MemberCookieEnums._MALL_FORM.getName(), true);
		manager.cleanAll(getResponse(), MemberCookieEnums._MALL_FORM_COMPANY.getName(), true);
		manager.cleanAll(getResponse(), MemberCookieEnums._MALL_FORM_SHOP.getName(), true);
	}
}
